package com.ecommerce.payment.dto;

import com.ecommerce.payment.entity.Payment;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 支付响应 DTO
 */
@Data
public class PaymentResponse {
    /**
     * 支付ID
     */
    private Long paymentId;

    /**
     * 支付单号
     */
    private String paymentNo;

    /**
     * 订单号
     */
    private String orderNo;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 支付方式
     */
    private String paymentMethod;

    /**
     * 支付状态
     */
    private String paymentStatus;

    /**
     * 支付金额
     */
    private BigDecimal amount;

    /**
     * 第三方支付单号
     */
    private String thirdPartyPaymentNo;

    /**
     * 支付完成时间
     */
    private LocalDateTime paymentTime;

    /**
     * 支付失败原因
     */
    private String failureReason;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 支付URL
     */
    private String paymentUrl;

    /**
     * 二维码
     */
    private String qrCode;

    /**
     * 从Payment实体创建PaymentResponse
     *
     * @param payment 支付实体
     * @return PaymentResponse
     */
    public static PaymentResponse fromPayment(Payment payment) {
        PaymentResponse response = new PaymentResponse();
        response.setPaymentId(payment.getId());
        response.setPaymentNo(payment.getPaymentNo());
        response.setOrderNo(payment.getOrderNo());
        response.setUserId(payment.getUserId());
        response.setPaymentMethod(payment.getMethod().name());
        response.setPaymentStatus(payment.getStatus().name());
        response.setAmount(new BigDecimal(payment.getAmount()).divide(new BigDecimal(100))); // 转换为元
        response.setThirdPartyPaymentNo(payment.getThirdPartyPaymentNo());
        response.setPaymentTime(payment.getPayTime());
        response.setFailureReason(payment.getFailureReason());
        response.setCreateTime(payment.getCreateTime());
        response.setUpdateTime(payment.getUpdateTime());
        return response;
    }

    /**
     * 设置支付URL
     *
     * @param paymentUrl 支付URL
     */
    public void setPaymentUrl(String paymentUrl) {
        this.paymentUrl = paymentUrl;
    }

    /**
     * 设置二维码
     *
     * @param qrCode 二维码
     */
    public void setQrCode(String qrCode) {
        this.qrCode = qrCode;
    }
}